package com.fram.controller.admin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.admin.wallet.request.WithdrawalRequest;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseAdminController;
import com.fram.entity.UserWithdrawCardNumber;
import com.fram.service.admin.WalletService;

//资金中心
@RestController
@RequestMapping("/admin/{version}/auth/wallet")
public class WalletController extends BaseAdminController {

	private static Logger LOG = LoggerFactory.getLogger(WalletController.class);
	@Autowired
	private WalletService walletService;

	//
	@ApiVersion(1.0)
	@RequestMapping("balance")
	private ResponseMsg balance() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(walletService.balance(getAdminUserId()));
		} catch (ProException e) {
			LOG.error("-后台显示金额出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台显示金额异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	// 展示 用户 提现卡
	@ApiVersion(1.0)
	@RequestMapping("card")
	private ResponseMsg card() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(walletService.card(getAdminUserId()));
		} catch (ProException e) {
			LOG.error("-后台银行卡展示出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台银行卡展示异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping("withdrawal")
	private ResponseMsg withdrawal(@RequestBody WithdrawalRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			walletService.withdrawal(request);
		} catch (ProException e) {
			LOG.error("-后台提现错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台提现异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	// 增加银行卡  
	@ApiVersion(1.0)
	@RequestMapping("card/add")
	private ResponseMsg add(@RequestBody UserWithdrawCardNumber request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getAdminUserId());
			walletService.cardAdd(request);
		} catch (ProException e) {
			LOG.error("-后台提现错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台提现异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	// 删除银行卡
	@ApiVersion(1.0)
	@RequestMapping("card/delete")
	private ResponseMsg delete(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			walletService.cardDelete(id);
		} catch (ProException e) {
			LOG.error("-后台提现错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台提现异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	// 详情银行卡
	@ApiVersion(1.0)
	@RequestMapping("card/detail")
	private ResponseMsg detail(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(walletService.cardDetail(id));
		} catch (ProException e) {
			LOG.error("-后台提现错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台提现异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	// 修改银行卡
	@ApiVersion(1.0)
	@RequestMapping("card/update")
	private ResponseMsg update(@RequestBody UserWithdrawCardNumber request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			walletService.cardUpdate(request);
		} catch (ProException e) {
			LOG.error("-后台提现错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台提现异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
